#include "SBSlowOnApproach.h"
#include "AIAgent.h"

SBSlowOnApproach::SBSlowOnApproach(AIAgent* __restrict agent, GameObject* __restrict target, float dist) : Behaviour(agent), m_stoppingDistance(dist)
{
	m_pTarget = target;
	m_stoppingDistanceSqr = dist*dist;
	m_debugDrawColour = Vec3f(0,1,0.2f);
}

Vec3f SBSlowOnApproach::Update()
{
	Vec3f slowingForce;
	Vec3f posDifference = m_pTarget->GetPos() - m_pAgent->GetPos();
	float targetDistanceSqr = posDifference.LengthSqr();
	if(targetDistanceSqr < m_stoppingDistanceSqr)
	{
		//Slowing force 
		// calculated by negating the vector between agent and target 
		// then scaled by the difference between the stopping distance and the distance from agent to target
		slowingForce = (posDifference * -1).TruncateReturn(m_stoppingDistance - posDifference.Length());

		return slowingForce;
	}
	else 
	{
		return Vec3f();
	}

}